#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : HtmlDownloader.py
# @Author: Xuhao
# @Date  : 2018/1/25
# @Desc  : 
from  app import config
import requests
import chardet

class Html_Downloader(object):
    @staticmethod
    def download(url):
        try:
            r = requests.get(url=url, headers=config.get_header(), timeout=config.TIMEOUT)
            r.encoding = chardet.detect(r.content)['encoding']
            if (not r.ok) or len(r.content) < 500:
                raise  ConnectionError
            else:
                return r.text
        except Exception:
            #这里后面还可以添加重试次数
            return None


if __name__ == "__main__":

    response = Html_Downloader.download('http://zhannei.baidu.com/cse/search?q=&s=1393206249994657467&srt=totalClick&nsid=0')

